﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using GMME.Util;
using UnityEngine;
using UnityEngine.Scripting;

namespace GMME.Model.Rtm.Result
{
    [DataContract]
    [Serializable]
    public class GetRtmChannelPropertiesResult : ErrorResult
    {
        /**
         * 开发者传入的频道Id
         */
        [DataMember(Name = "channelId")]
        [Preserve]
        public string ChannelId { get; set; }

        /**
         * RTm频道内玩家信息
         */
        [DataMember(Name = "channelProperties")]
        [Preserve]
        public Dictionary<string, string> ChannelProperties { get; set; }

        [DataMember(Name = "channelPropertiesInner")]
        [Preserve]
        public List<Property> ChannelPropertiesInner { get; set; }

#if UNITY_ANDROID
        internal static GetRtmChannelPropertiesResult ConvertGetRtmChannelPropertiesResult(AndroidJavaObject javaObject)
        {
            if (javaObject != null)
            {
                GetRtmChannelPropertiesResult getRtmChannelPropertiesResult = new();
                getRtmChannelPropertiesResult.ChannelId = javaObject.Call<string>("getChannelId");
                getRtmChannelPropertiesResult.Code = javaObject.Call<int>("getCode");
                getRtmChannelPropertiesResult.Msg = javaObject.Call<string>("getMsg");
                getRtmChannelPropertiesResult.ChannelProperties =
                    TransferUtil.MapToDict(javaObject.Call<AndroidJavaObject>("getChannelProperties"));
                return getRtmChannelPropertiesResult;
            }

            return null;
        }
#endif

        public void ConvertPropertiesToDict()
        {
            var dict = new Dictionary<string, string>();
            if (ChannelPropertiesInner != null)
                foreach (var property in ChannelPropertiesInner)
                    dict.Add(property.Key, property.Value);

            ChannelProperties = dict;
        }
    }
}